// 微页面相关组件
var Common = {
  formatTime: function (sec) {
    var sec = Math.ceil(sec)
    return Math.floor(sec/60) + ':' + String.prototype.padStart.call(sec%60, 2, 0)
  },
  hasClass: function (ele, str) {
    var reg = new RegExp('\\b' + str + '\\b')
    return !!ele.className.match(reg)
  },
  addClass: function (ele, str) {
    if(!this.hasClass(ele, str)) {
      ele.className = ele.className + ' ' + str
    }
  },
  removeClass: function (ele, str) {
    var reg = new RegExp('\\b' + str + '\\b')
    ele.className = ele.className.replace(reg, '').trim()
  }
}
// 音频播放器
;(function () {
  var $players = document.querySelectorAll('.audio_player');
  $players.forEach(function($player){
    var $icon = $player.querySelector('.player_icon'),
        $progress = $player.querySelector('.progress'),
        $control = $player.querySelector('.progress_inner'),
        $curTime = $player.querySelector('.curTime'),
        $time = $player.querySelector('.time'),
        $audio = $player.querySelector('audio');
    var loop = setInterval(function () {
      $curTime.innerHTML = Common.formatTime($audio.currentTime)
      var percent = $audio.currentTime/$audio.duration
      $control.style.transform = 'translateX(' + percent*100 + '%)'
    }, 1000)
    $icon.addEventListener('touchstart', function () {
      if ($audio.paused) {
        $audio.play()
      } else {
        $audio.pause()
      }
    })
    $audio.addEventListener('loadedmetadata', function () {
      $time.innerHTML = Common.formatTime($audio.duration)
    })
    $audio.addEventListener('play', function () {
      Common.addClass($player, 'play')
    })
    $audio.addEventListener('ended', function () {
      Common.removeClass($player, 'play')
    })
    $audio.addEventListener('pause', function () {
      Common.removeClass($player, 'play')
    })
    $progress.addEventListener('touchstart', function (e) {
      var $target = e.target;
      var curLength = e.touches[0].pageX - $progress.offsetLeft
      var percent = curLength/$progress.offsetWidth
      $audio.currentTime = $audio.duration * percent
    })

  })
})()
// banner轮播图
;(function () {
  if (document.querySelector('.swiper-container')) {
    new Swiper('.swiper-container', {
      autoplay: true,//可选选项，自动滑动
      pagination: {
        el: '.swiper-pagination'
      }
    })
  }
})()
// 名片
;(function () {
  var $cards = document.querySelectorAll('.we_card')
  if ($cards.length) {
    $cards.forEach(function ($card) {
      var $id = document.querySelector('.card_uid'),
          $avatar = $card.querySelector('.avatar'),
          $map = $card.querySelector('.map'),
          $name = $card.querySelector('.name'),
          $addr = $card.querySelector('.addr'),
          $phone = $card.querySelector('.phone');
      fetch('/cloud/online/architecture/card/user?userId=' + $id.value)
        .then(function (res) {
          return res.json()
        })
        .then(function (json) {
          var data = json.data
          if(json.code === 1) {
            $avatar.src = data.avatar;
            $name.innerHTML = data.username;
            $addr.innerHTML = data.address;
            $phone.innerHTML = data.mobile;
          }
        })
    })
  }
})()
